package com.example.elmspringboot.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.elmspringboot.domain.Orders;
import com.example.elmspringboot.mapper.OrderMapper;
import com.example.elmspringboot.service.OrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrderService {
    @Resource
    private OrderMapper orderMapper;
    @Override
//    可拓展筛选条件
    public IPage<Orders> getPage(int currentPage, int pageSize) {
        IPage<Orders> page = new Page(currentPage, pageSize);
        return orderMapper.selectPage(page,null);
    }

    @Override
    public Integer insertDetails(Orders order) {
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
//      我只用找orderState为0的,已支付的不算做未支付
        wrapper.eq("user_id", order.getUserId()).eq("shop_id", order.getShopId()).eq("order_state", 0);
        Orders orderEntity = orderMapper.selectOne(wrapper);
        if(orderEntity != null){
            //将订单修改为现在的
            orderEntity.setOrderTotal(order.getOrderTotal());
            orderMapper.update(orderEntity, new QueryWrapper<Orders>().eq("id", orderEntity.getId()));
            return orderEntity.getId();
        }
        else {
            orderMapper.insert(order);
            QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", order.getUserId()).eq("shop_id", order.getShopId());
            return orderMapper.selectOne(queryWrapper).getId();
        }

    }

    @Override
    public List<Orders> getBusinessOrders(Integer shopId) {
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.eq("shop_id", shopId);
        List<Orders> orders = orderMapper.selectList(wrapper);
        return orders;
    }
}
